/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package data;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;

/**
 *
 * @author miharvil
 */
public class RecipientHandler {
    Login login;
    String server;
    ArrayList<Recipient> recipients = new ArrayList<Recipient>();
    
    public RecipientHandler(Login login, String server)
    {
        this.login = login;
        this.server = server;
    }
    
    public void reload()
    {
        ObjectInputStream in = null;
        ObjectOutputStream out = null;
        
        try 
        { 
            //open socket
            Socket s = new Socket(server, 8189);
            
            //output stream
            out = new ObjectOutputStream(s.getOutputStream());
            
            //send to server
            out.writeObject(login);
            out.flush();
            
            //input stream
            in = new ObjectInputStream(s.getInputStream());
            
            //login response
            login = (Login) in.readObject();
            
            if(login.getAuth())
            {
                //recipient list
                recipients = (ArrayList<Recipient>) in.readObject();  
            }

            in.close();
            s.close();
        }
        
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    
    public void setLogin(Login login)
    {
        this.login = login;
    }
    
    public void setServer(String server)
    {
        this.server = server;
    }
    
    public String getServer()
    {
        return server;
    }
    
    public Login getLogin()
    {
        return login;
    }
    
    public ArrayList<Recipient> getRecipients()
    {
        return recipients;
    }
}
